package light.mvc.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.model.sys.EmployInfoBO;
import light.mvc.pageModel.sys.EmployInfoVO;
import light.mvc.service.sys.EmployInfoServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class EmployInfoServiceImpl implements EmployInfoServiceI {

   @Autowired
   private BaseDaoI<EmployInfoBO> EmployInfoDao;

   @Override
   public void add(EmployInfoVO r) {
       EmployInfoBO t = new EmployInfoBO();
       t.setEmployName(r.getEmployName());
       t.setEmployMobiel(r.getEmployMobiel());
       t.setEmployEmail(r.getEmployEmail());
       t.setEmployQQ(r.getEmployQQ());
       t.setEmployAddress(r.getEmployAddress());
       t.setEmployIdCard(r.getEmployIdCard());
       t.setEmployBirthday(r.getEmployBirthday());
       t.setEmployNote(r.getEmployNote());
       EmployInfoDao.save(t);
   }

   @Override
   public void delete(Long id) {
       EmployInfoBO t = EmployInfoDao.get(EmployInfoBO.class, id);
       EmployInfoDao.delete(t);
   }

   @Override
   public void edit(EmployInfoVO r) {
       EmployInfoBO t = EmployInfoDao.get(EmployInfoBO.class, r.getId());
       t.setEmployName(r.getEmployName());
       t.setEmployMobiel(r.getEmployMobiel());
       t.setEmployEmail(r.getEmployEmail());
       t.setEmployQQ(r.getEmployQQ());
       t.setEmployAddress(r.getEmployAddress());
       t.setEmployIdCard(r.getEmployIdCard());
       t.setEmployBirthday(r.getEmployBirthday());
       t.setEmployNote(r.getEmployNote());
       EmployInfoDao.update(t);
   }
   
   public List<EmployInfoVO> findByEmployInfoVOList(String id){
	   List<EmployInfoVO> listEmployInfoVO=new ArrayList<EmployInfoVO>();
	   List<EmployInfoBO> listEmployInfoBO=new ArrayList<EmployInfoBO>();
	   listEmployInfoBO=EmployInfoDao.find("from EmployInfoBO where id in("+id+")");
	   if(listEmployInfoBO.size()>0){
		   for(EmployInfoBO employInfoBO:listEmployInfoBO){
			   EmployInfoVO employInfoVO=new EmployInfoVO();
			   BeanUtils.copyProperties(employInfoBO, employInfoVO);
			   listEmployInfoVO.add(employInfoVO);
		   } 
	   }
	   return listEmployInfoVO;
   }

   @Override
   public EmployInfoVO get(Long id) {
       EmployInfoBO t = EmployInfoDao.get(EmployInfoBO.class, id);
       EmployInfoVO r = new EmployInfoVO();
       r.setEmployName(t.getEmployName());
       r.setEmployMobiel(t.getEmployMobiel());
       r.setEmployEmail(t.getEmployEmail());
       r.setEmployQQ(t.getEmployQQ());
       r.setEmployAddress(t.getEmployAddress());
       r.setEmployIdCard(t.getEmployIdCard());
       r.setEmployBirthday(t.getEmployBirthday());
       r.setEmployNote(t.getEmployNote());
       return r;
   }

   @Override
   public List<EmployInfoVO> dataGrid(EmployInfoVO employInfo, PageFilter ph) {
       List<EmployInfoVO> ul = new ArrayList<EmployInfoVO>();
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from EmployInfoBO t ";
       List<EmployInfoBO> l = EmployInfoDao.find(hql + whereHql(employInfo, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
       for (EmployInfoBO t : l) {
           EmployInfoVO u = new EmployInfoVO();
           BeanUtils.copyProperties(t, u);
           ul.add(u);
       }
       return ul;
   }

   @Override
   public Long count(EmployInfoVO employInfo, PageFilter ph) {
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from EmployInfoBO t ";
       return EmployInfoDao.count("select count(*) " + hql + whereHql(employInfo, params), params);
   }

   private String whereHql(EmployInfoVO employInfo, Map<String, Object> params) {
       String hql = "";
       if (employInfo != null) {
           hql += " where 1=1 ";
           if (employInfo.getId() != null) {
               hql += " and t.name like :name";
               params.put("name", "%%" + employInfo.getId() + "%%");
           }
       }
       return hql;
   }

   private String orderHql(PageFilter ph) {
       String orderString = "";
       if ((ph.getSort() != null) && (ph.getOrder() != null)) {
           orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
       }
       return orderString;
   }

}